package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import pojo.Province;
import util.DBHelper;
import dao.ProvinceDao;

public class ProvinceDaoImpl implements ProvinceDao{
	public List<Province> queryAllProvinceByName(String province) {
		Connection con = DBHelper.getConnectionWithDs();
		String sql = "select id, provinceid, province from provinces ";
		if(province!=null){
			 sql+="where province like '%"+province+"%' ";
		}
		
		Province p = null;
		List<Province> list = new ArrayList<Province>();
		PreparedStatement pst = null;
		ResultSet rst = null;
		try {
			pst =  con.prepareStatement(sql);
			rst = pst.executeQuery();
		
			while(rst.next()){
				p = new Province();
				p.setId(rst.getInt(1));
				p.setProvinceid(rst.getString(2));
				p.setProvince(rst.getString(3));
				list.add(p);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBHelper.close(pst, rst, con);
		}
		return list;
	}
}
